package com.shujiaotong.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shujiaotong.common.R;
import com.shujiaotong.pojo.Tags;
import com.shujiaotong.service.ITagsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/tags")
@Slf4j
public class TagsController {

    @Autowired
    private ITagsService tagsService;

    @Autowired
    private RedisTemplate redisTemplate;

    // 分页
    @GetMapping("page")
    public R page(
            @RequestParam int page,
            @RequestParam int pageSize,
            @RequestParam(required = false) String name) {
        log.info("当前页:{}, 页条数:{}, 标签:{}", page, pageSize, name);

        Page<Tags> tagsPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Tags> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name), Tags::getName, name);
        tagsService.page(tagsPage, qw);

        return R.success(tagsPage);
    }

    // 新增标签
    @PostMapping
    public R save(@RequestBody Tags tags) {
        redisTemplate.delete("tags");
        log.info("新增标签:{}", tags);
        boolean save = tagsService.save(tags);

        if (save) {
            updateTagsCache();
        }

        return save ? R.success("新增成功") : R.error("新增失败");
    }

    // 根据id查询
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        log.info("根据id查询标签:{}", id);
        Tags tags = tagsService.getById(id);
        return R.success(tags);
    }

    // 修改标签
    @PutMapping
    public R update(@RequestBody Tags tags) {
        log.info("修改标签:{}", tags);
        boolean updated = tagsService.updateById(tags);

        if (updated) {
            updateTagsCache();
        }

        return updated ? R.success("修改成功") : R.error("修改失败");
    }

    // 删除标签
    @DeleteMapping
    public R delete(Long id) {
        log.info("删除标签:{}", id);
        boolean deleted = tagsService.removeById(id);

        if (deleted) {
            updateTagsCache();
        }

        return deleted ? R.success("删除成功") : R.error("删除失败");
    }

    // 查询所有
    @GetMapping
    public R list() {
        Object tags = redisTemplate.opsForValue().get("tags");
        List<Tags> list = new ArrayList<>();

        if (!StringUtils.isEmpty(tags)) {
            list = (List<Tags>) tags;
        } else {
            list = tagsService.list();
            redisTemplate.opsForValue().set("tags", list);
        }

        return R.success(list);
    }

    // 更新缓存
    private void updateTagsCache() {
        List<Tags> list = tagsService.list();
        redisTemplate.opsForValue().set("tags", list);
    }

}
